<!DOCTYPE html>
<html lang="en">

	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<!-- STYLESHEETS -->
		<!--[if lt IE 9]><script src="../../js/flot/excanvas.min.js"></script><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
		<link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
		<link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
		<link rel="stylesheet" type="text/css" href="../../css/responsive.css">

		<link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
		<!-- file-input -->
		<link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css" />
		<!-- SELECT2 -->
		<link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css" />
		<!-- UNIFORM -->
		<link rel="stylesheet" type="text/css" href="../../js/uniform/css/uniform.default.min.css" />
		<!-- datatable -->
		<link rel="stylesheet" href="../../js/datatables/datatable.css">
		<!-- WIZARD -->
		<link rel="stylesheet" type="text/css" href="../../js/bootstrap-wizard/wizard.css" />
		  <link rel="stylesheet" href="../../js/datepicker/datepicker.css">
		<!-- FONTS
	<link href='http://fonts.useso.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
-->
		<!-- JQUERY -->
		<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
		<script src="../../js/common.js"></script>
	</head>

	<body>
		<header class="navbar clearfix" id="header">
		</header>

		<!-- PAGE -->
		<section id="page">
			<!-- SIDEBAR -->
			<div id="sidebar" class="sidebar">
				<div class="sidebar-menu nav-collapse">

					<!-- SIDEBAR MENU -->
					<ul>

					</ul>
					<!-- /SIDEBAR MENU -->
				</div>

			</div>
			<!-- /SIDEBAR -->
			<div id="main-content">

				<div class="container">
					<div class="row">
						<div id="content" class="col-lg-12">
							<!-- PAGE HEADER-->
							<div class="row">
								<div class="col-sm-12">
									<div class="page-header">
										<!-- STYLER -->

										<!-- /STYLER -->
										<!-- BREADCRUMBS -->
										<ul class="breadcrumb">
											<li>
												<i class="fa fa-home"></i>
												<a href="index.html">首页</a>
											</li>
											<li>
												<a href="#">商品管理</a>
											</li>
											<li>维护商品</li>
										</ul>
										<!-- /BREADCRUMBS -->

									</div>
								</div>
							</div>
							<!-- /PAGE HEADER -->
							<!-- SAMPLE -->
							<form id="form_coupon">
							<div class="row col-md-12">
								<div class="col-md-3" style="width: 25%;height:200px">
									<div class="" style="text-align: center;margin-top:30%;font-size:24px;">基本信息</div>
								</div>
								<div class="col-md-9">
									<div class="box border primary" style="">
										<div class="form-group mrg-bt10-right" style="margin-top:10px;">
											<label class="control-label col-md-3"><span class="required">*</span>供应商名称：</label>
											<div class="col-md-8 inline">
												<select id="supplier_select" class="col-md-12 select2-offscreen" tabindex="-1">
															
												</select>	
											</div>
										</div>
										<div class="form-group mrg-bt10-right" style="margin-top:10px;">
											<label class="control-label col-md-3"><span class="required">*</span>合同编号：</label>
											<div class="col-md-8 inline">
												<select id="contract_select" class="col-md-12 select2-offscreen" tabindex="-1">
															
												</select>	
											</div>
										</div>
										
										<div class="form-group mrg-bt10-right">
											<label class="control-label col-md-3"><span class="required">*</span>结算类型：</label>
											<div class="col-md-8 inline">
												<input type="radio" checked="checked" value="PreMoney" name="settlementMode">预付款
												<input type="radio" value="SecondMoney"  name="settlementMode">提货款
												<input type="radio" value="LastMoney"  name="settlementMode">尾款
												<input type="radio" value="DepositMoney"  name="settlementMode">保证金
											</div>
										</div>
										<div class="form-group mrg-bt10-right rate" style="margin-top:10px;">
											<label class="control-label col-md-3">比例：</label>
											<div class="col-md-8 inline " >
												
											</div>
										</div>
										<div class="form-group mrg-bt10-right purchaselist" >
											<label class="control-label col-md-3"><span class="required">*</span>进货单号：</label>
											<div class="col-md-8 inline">
												<select id="purchase_select" class="col-md-12 select2-offscreen" tabindex="-1">
															
												</select>	
											</div>
										</div>
										
										<div class="form-group mrg-bt10-right" style="margin-top:10px;">
											<label class="control-label col-md-3">合同金额：</label>
											<div class="col-md-8 inline contractPrice" >
												
											</div>
										</div>
										<div class="form-group mrg-bt10-right">
											<label class="control-label col-md-3"><span class="required">*</span>申请金额:</label>
											<div class="col-md-8 inline" style="text-align: left">
												<input type="text" class="form-control" style="width:130px" name="requestPrice" onkeypress="return $.IsDecimal(event);" onblur="return $.IsMoney(this);"  onfocus="this.select()" onmouseover="this.onfocus()" placeholder="请输入申请金额" maxlength="10" />
											</div>
										</div>
										<div class="form-group mrg-bt10-right" style="height: 80px">
											<label class="control-label col-md-3">申请说明：</label>
											<div class="col-md-8 inline">
												<textarea name="requestRemark" class="form-control" style="width:480px;resize:none;" ></textarea>
												
											</div>
										</div>
										<div class="form-group mrg-bt10-right settlementRemark" style="display:none;height: 100px;margin-top:60px">
											<label class="control-label col-md-3">驳回说明：</label>
											<div class="col-md-8 inline">
												<textarea name="settlementRemark" readonly=true class="form-control" style="width:480px;resize:none;" ></textarea>
												
											</div>
										</div>
									</div>
								</div>
							</div>
								
								<div class="row" style="height: 30px;">
								<div class="col-md-12" style="text-align: center;">
									<div class="box ">
										<a href="#"  class="btn btn-success config bt-submit" style="width: 100px">
											保存
										</a>
									</div>
								</div>
					    </div>
							</form>
							<!-- /SAMPLE -->

							<div class="footer-tools">
								<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
							</div>
						</div>
						<!-- /CONTENT-->
					</div>
				</div>
			</div>
		</section>
		<!--/PAGE -->
		<!-- JAVASCRIPTS -->
		<!-- Placed at the end of the document so the pages load faster -->

		<!-- BOOTSTRAP -->
		<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>

		<!-- BLOCK UI -->
		<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
		<!--bootbox-->
		<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
		<!-- AUTOSIZE -->
		<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
		<!-- spinner -->
		<script src="../../js/spinner/spin.js"></script>
		<!-- INPUT MASK -->
		<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>

		<!-- SELECT2 -->
		<script type="text/javascript" src="../../js/select2/select2.min.js"></script>

        <script type="text/javascript" src="../../js/datepicker/datepicker.js"></script>
        <script src="../../js/jquery-validate/jquery.validate.min.js"></script>
		<!-- COOKIE -->
		<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
		<!-- CUSTOM SCRIPT -->
		<script src="../../js/script.js"></script>

		<script src="../../js/Sortable.min.js"></script>
		<script>
			var AddSettlement = function() {
			var id=null;
			var settlement={};
			var saveSettlement=function(){
				  settlement.requestRemark=$("[name=requestRemark]").val();
		          settlement.requestPrice=$("input[name=requestPrice]").val();
				if(id==null){
	            settlement.contractCode=$("#contract_select").val();
	            settlement.supplierId=$("#supplier_select").val();
	            settlement.settlementMode=$("[name=settlementMode]:checked").val();
	            settlement.purchaseCode=$("#purchase_select").val()=="-1"?"":$("#purchase_select").val();
	          
	            if(settlement.contractCode==null){
	            	$.alert("请选择合同编号");
	            	return false;
	            }
	            if(settlement.supplierId==null){
	            	$.alert("请选择供应商");
	            	return false;
	            }
	       
	            if(settlement.settlementMode=="SecondMoney" &&settlement.purchaseCode==""){
	            	$.alert("请选择进货单号");
	            	return false;
	            }
				}
	            if(settlement.requestPrice==null||settlement.requestPrice==0){
	            	$.alert("请输入申请金额");
	            	return false;
	            }
	            
	            $.confirm("确认提交结算?",function(){
	               $.blockUI();
	               $.ajax({
						type: "post",
						url: App.getContextPath() + "admin/settlement/saveSettlement.do",
						data:$.param({settlement:JSON.stringify(settlement)}),
						async: true,
						success: function(result) {
						 $.unblockUI();
							location.href="settlement_list.html";
							}
					});
	            });
			};
			
			var getSupplier=function(){
				$.ajax({
					type: "post",
					url: App.getContextPath()+"admin/supplier/searchSupplierList.do",
					async: true,			
					success: function(result) {
						if(result!=null)
						{ 										
							var option="<option value=-1>选择供应商</option>";
							$.each(result,function(index,item){								
								 option+="<option value='"+item.id+"'>"+item.supplierName+"</option>";
							});
							$("#supplier_select").empty().append(option).select2({placeholder: "选择供应商"});
							
						}
						
						
					}
				});
			}
			var getContractBySupplier=function(){
				$.ajax({
					type: "post",
					url: App.getContextPath()+"admin/contract/findContractListBySupplier.do?supplierId="+$("#supplier_select").val(),
					async: true,			
					success: function(result) {
						if(result!=null)
						{ 
										
							var option="<option value=-1>选择合同编号</option>";
							$.each(result,function(index,item){								
								 option+="<option value='"+item.contractCode+"' price='"+item.contractPrice+"' premoney='"+item.preMoney+","+item.prePercent+"' pickmoney='"+item.pickMoney+","+item.pickPercent+"' endmoney='"+item.endMoney+","+item.endPercent+"' earnestmoney='"+item.earnestMoney+"'>"+item.contractCode+"</option>";
							});
							$("#contract_select").empty().append(option).select2({placeholder: "选择合同编号"});
							
						}
						
					}
				});
			}
			
			var getSettlement=function(){
				$.ajax({
					type: "post",
					url: App.getContextPath()+"admin/settlement/getSettlementById.do",
					async: true,	
					data:{id:id},
					success: function(result) {
						 $("#supplier_select").parent().html(result.supplier.supplierName);
						 $("#contract_select").parent().html(result.contractCode);
						 $(".contractPrice").html(format_thousands(result.contract.contractPrice));
						 var type='预付款';
						 var rate=result.contract.prePercent*100+"%";

						 if(result.settlementMode=="SecondMoney"){type='提货款';rate=result.contract.pickPercent*100+"%"}
						 if(result.settlementMode=="LastMoney"){type='尾款';rate=result.contract.endPercent*100+"%"}
						 if(result.settlementMode=="DepositMoney"){type='保证金'; $(".rate").hide();}
						 $("[name=settlementMode]").parent().html(type);
						 $(".rate>div").html(rate);
						 if(result.settlementMode=="SecondMoney"){									
							 $("#purchase_select").parent().html(result.purchaseCode);
							 $(".purchaselist").show();
							// $(".rate").hide();
						 }else{
							 $(".purchaselist").hide();
							
						 }
						
						 $("[name=requestPrice]").val(result.requestPrice);
						 $("[name=requestRemark]").val(result.requestRemark);
						 if(result.settlementStatus=="Abort"){
							 $("[name=settlementRemark]").val(result.settlementRemark);
							 $(".settlementRemark").show();
						 }
						   settlement.contractCode=result.contractCode;
				            settlement.supplierId=result.supplierId;
				            settlement.settlementMode=result.settlementMode;
				            settlement.purchaseCode=result.purchaseCode;
				            settlement.id=id;
				            
					}
				});
			}
			

				return {
					init: function() {
						 id=$.getUrlParam("id");
				   if(id==null){
				       $("[name=settlementMode]").unbind("change").change(function(){
				    	   var mode=$("[name=settlementMode]:checked").val();
				    	  if(mode=="DepositMoney")$(".rate").hide();
				        	  var contractCode=$("#contract_select").val();
				        	  if(mode=="SecondMoney"&&contractCode!=null){
				        		  $(".purchaselist").show();
				        		  var money=$("#contract_select option:selected").attr("pickmoney");
					        	  var rate=money.split(",")[1];
				        		   $(".rate>div").html((money.split(",")[1]*100)+"%");
				        			
				        		
				        		  $.ajax({
				  					type: "post",
				  					url: App.getContextPath()+"admin/purchase/getPurchaseList.do?contractCode="+contractCode,
				  					async: true,			
				  					success: function(result) {
				  						if(result!=null)
				  						{ 
				  										
				  							var option="<option value=-1>选择进货单号</option>";
				  							$.each(result,function(index,item){								
				  								 option+="<option value='"+item.purchaseCode+"' price="+item.inStockPrice+">"+item.purchaseCode+"</option>";
				  							});
				  							$("#purchase_select").empty().append(option).select2({placeholder: "选择进货单号"});
				  							
				  						}
				  						
				  					}
				  				});
				        		   $("#purchase_select").change(function(){
				        			   var v=$(this).val();
				        			   if(v!=-1){
				        				   $("[name=requestPrice]").val( (rate*$("#purchase_select").find("option:selected").attr("price")).toFixed(2));
				        			   }
				        		   });
									   
			        		  
			        	  }else{
			        		  $(".purchaselist").hide();
			        		  var str="premoney";
			        		 if(mode=="LastMoney")
			        			 str="endmoney";
			        		 if(mode=="DepositMoney")
			        			 str="earnestmoney";
			        		 var money=$("#contract_select option:selected").attr(""+str+"");
			        		 
			        		  if(id==null){
			        			  $("[name=requestPrice]").val(money.split(",")[0]);
			        			  if(mode!="DepositMoney"){
			        				  $(".rate>div").html((money.split(",")[1]*100)+"%");
			        				  $(".rate").show();
			        			  }
			        		  	  
			        		  }
			        	  }
			          })
			          $("[name=requestPrice]").val('');
					  $("[name=settlementMode]:eq(0)").prop("checked",true);						
					   $(".purchaselist").hide();
					   getSupplier();
					   $("#supplier_select").change(getContractBySupplier);
					   $("#contract_select").change(function(){
						     // var money=$("#contract_select option:selected").attr("premoney");				        	 
				        	  //$(".rate>div").html((money.split(",")[1]*100)+"%");
				        	  var str="premoney";
						   if($(this).val()!=null){							  
							   $(".contractPrice").html(format_thousands($("#contract_select option:selected").attr("price")));
						   var mode=$("[name=settlementMode]:checked").val();
						   if(mode=="LastMoney")
			        			 str="endmoney";
			        		 if(mode=="DepositMoney")
			        			 str="earnestmoney";
			        		 if(mode=="SecondMoney")
			        			 str="pickmoney";
			        		 var money=$("#contract_select option:selected").attr(""+str+"");
			        		 console.log(money);
			        		 $(".rate>div").html((money.split(",")[1]*100)+"%");
							if(mode!="DepositMoney")
							   $("[name=requestPrice]").val(money.split(",")[0]);
						   
						   if(mode=="SecondMoney")
							   $("[name=settlementMode]:eq(1)").trigger("change");
						   }
					   });
					
				   }else{
					   getSettlement();
				   }
				        App.init();
						$(".bt-submit").click(saveSettlement);
					}
				}
			}();
			$(function() {
				$("#header").load("../head.html");
				AddSettlement.init();
			});
		</script>

		<!-- /JAVASCRIPTS -->
	</body>

</html>